<?php

if (!class_exists('OracleDBConnection')) {

    class OracleDBConnection {

        private $conn;
        private $username = 'KECO_DEV';
        private $password = 'ghksrudrhdeks';
        private $db_info = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 110.45.147.175)(PORT = 1521)) (CONNECT_DATA = (SID = orcl)) )";
        var $result;
        var $error = false;
        private $transaction = false;
        private $stmt;
        private $charset = 'UTF8';

        public function OracleDBConnection($username = '', $password = '', $db_info = '', $charset = '') {
            if ($username) {
                $this->username = $username;
            }
            if ($password) {
                $this->password = $password;
            }
            if ($db_info) {
                $this->db_info = $db_info;
            }
            $this->conn = oci_connect($this->username, $this->password, $this->db_info, $this->charset);
            if (!$this->conn) {
                die('오라클 커넥션 에러 : ' . oci_error());
            }
        }

        function startTransaction() {
            $this->transaction = true;
        }

        function commit() {
            if ($this->transaction) {
                if ($this->error) {
                    oci_rollback($this->conn);
                } else {
                    oci_commit($this->conn);
                }
            }
            $this->transaction = false;
        }

        //사용법 : $db->query("INSERT INTO OCI_TEST (OCINO, OCITITLE) VALUES (SEQ_OCITEST.NEXTVAL, :val )" , array( 'val' => 'test1'));
        function query($query = '', $data = array(), $idx = 0) {
            $query = trim($query);
            if (!$query) {
                die('ERROR: empty query...');
            }
            $this->stmt = oci_parse($this->conn, $query);
            foreach ($data as $key => $value) {
                oci_bind_by_name($this->stmt, $key, $data[$key]);
            }            
            $excuted = oci_execute($this->stmt, OCI_DEFAULT);

            if ($excuted) {
                $this->error = false;
            } else {
                $this->error = true;
            }
            $type = strtolower(substr($query, 0, 6));
            //echo $type;
            switch ($type) {
                case 'select' :
                    $this->result[$idx] = $this->stmt;
                    break;
                case 'insert' :
                case 'update' :
                case 'delete' :
            }
        }

        function fetch($idx = 0) {
            return $this->result[$idx] ? oci_fetch_array($this->stmt, OCI_RETURN_NULLS+OCI_RETURN_LOBS) : null;
        }
        
        function fetchAll($idx = 0){
            $result = array();
            while($row = $this->fetch($idx)){
                $result[] = $row;
            }
            return $result;
        }

        function __destruct() {
            if ($this->stmt) {
                @oci_free_statement($this->stmt);
            }
            if ($this->conn) {
                @oci_close($this->conn);
            }
        }

        public function get_current_seq_val($seqname) {
            $this->query("SELECT {$seqname}.CURRVAL FROM DUAL");
            $row = $this->fetch();
            return $row[0];
        }

    }

}
?>
